package com.baoai.util;


import com.baoai.common.ArrayQuery;
import com.baoai.common.BeanFactory;
import com.baoai.common.StringWords;
import org.apache.lucene.document.*;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.lang.reflect.Method;


/**
 * Created by xiaren on 2017/4/6.
 */
public class DocumentUtils {

    public static final String CLASS_NAME = "className";

    public static Document getDocumentByObj(Object sourceObj) throws IOException {
        Document document = new Document();
        java.lang.reflect.Field[] sourceFields = sourceObj.getClass().getDeclaredFields();
        for (java.lang.reflect.Field field : sourceFields){
            Object value = BaseUtil.getFieldValueByName(field.getName(), sourceObj);
            if(StringUtils.isEmpty(value)){ // 非空才加入
                continue;
            }
            if(field.getType().equals(String.class)){   // string类型
                if(field.isAnnotationPresent(ArrayQuery.class)){
                    String[] array = BaseUtil.json2Arrays(value.toString());
                    ArrayQuery arrayQuery = field.getAnnotation(ArrayQuery.class);
                    document.add(new Field(arrayQuery.lengthName(), String.valueOf(array.length), TextField.TYPE_STORED));
                    for (int i = 0; i < array.length; i++){
                        document.add(new Field(field.getName() + i, array[i], TextField.TYPE_STORED));
                    }
                } else if(field.isAnnotationPresent(StringWords.class)){    // 不拆分的字段
                    document.add(new StringField(field.getName(), String.valueOf(value), StringField.Store.YES));
                } else {
                    document.add(new Field(field.getName(), String.valueOf(value), TextField.TYPE_STORED));
                }
            } else if(field.getType().equals(Integer.class)){
                LuenceUtils.addIntPoint(document, field.getName(), Integer.valueOf(value.toString()));
            } else if(field.getType().equals(Long.class)){
                LuenceUtils.addLongPoint(document, field.getName(), Long.valueOf(value.toString()));
            } else if(field.getType().equals(Double.class)){
                LuenceUtils.addDoublePoint(document, field.getName(), Double.valueOf(value.toString()));
            }
        }

        Repository repository = sourceObj.getClass().getAnnotation(Repository.class);   // 将类的类型存入，辨别类型
        String className = repository.value();
        document.add(new Field(CLASS_NAME, className, TextField.TYPE_STORED));

        return document;
    }

    public static Object getObjByDocument(Document document) {
        String className = document.get(CLASS_NAME);
        Object obj = null;
        try {
            obj = BeanFactory.getBean(className).getClass().newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        java.lang.reflect.Field[] sourceFields = obj.getClass().getDeclaredFields();
        for (java.lang.reflect.Field field : sourceFields){
            if(field.isAnnotationPresent(ArrayQuery.class)){
                ArrayQuery arrayQuery = field.getAnnotation(ArrayQuery.class);
                Object documentValue = document.get(arrayQuery.lengthName());
                if(documentValue == null){
                    continue;
                }
                Integer count = Integer.valueOf(documentValue.toString());
                String[] array = new String[count];
                for (int i = 0; i < count; i++){
                    array[i] = document.get(field.getName() + i);
                }
                String jsonArray = BaseUtil.arrays2Json(array);
                BaseUtil.setFieldValueByName(field.getName(), field.getType(), jsonArray, obj);
            } else {
                Object value = document.get(field.getName());
                if(StringUtils.isEmpty(value)){
                    continue;
                }
                if(field.getType().equals(Double.class)){
                    value = Double.valueOf(value.toString());
                } else if(field.getType().equals(Integer.class)){
                    value = Integer.valueOf(value.toString());
                } else if(field.getType().equals(Long.class)){
                    value = Long.valueOf(value.toString());
                }
                BaseUtil.setFieldValueByName(field.getName(), field.getType(), value, obj);
            }
        }
        return obj;
    }





}
